//
//  RwdWelcomeViewController.swift
//  Weibo_rwd
//
//  Created by 任文迪 on 16/7/12.
//  Copyright © 2016年 spaceX. All rights reserved.
//

import UIKit
import SDWebImage

class RwdWelcomeViewController: UIViewController {
    
    //懒加载控件
    private lazy var backgroundView: UIImageView = {
        
        let bgView = UIImageView(image: UIImage(named: "ad_background"))
        
        return bgView
    }()
    
    //头像
    private lazy var headView: UIImageView = {
        
        var headView = UIImageView(image: UIImage(named: "avatar_default_big"))
        headView.layer.cornerRadius = 42.5
        headView.layer.masksToBounds = true
        
        if ((RwduserAccountViewModel.shareUserAccount.userAccount?.avatar_large) != nil) {
            
            headView.sd_setImageWithURL(NSURL(string: (RwduserAccountViewModel.shareUserAccount.userAccount?.avatar_large)!), placeholderImage: UIImage(named: "avatar_default_big"))
            
        }else {
            
            headView = UIImageView(image: UIImage(named: "avatar_default_big"))
            
        }
        
        return headView
        
    }()
    
    //下方标签
    private lazy var welcomeLabel:UILabel = {
        
        let welcomeLabel = UILabel()
        

        welcomeLabel.font = UIFont.systemFontOfSize(15)
        welcomeLabel.textColor = UIColor.grayColor()
        welcomeLabel.alpha = 0
        
        
        if let name = RwduserAccountViewModel.shareUserAccount.userAccount?.name {
            
            welcomeLabel.text = "欢迎回来,\(name)"
            
        }else {
            
                    welcomeLabel.text = "欢迎回来"
            
        }
        
        return welcomeLabel
    }()
    //重写loadview方法 使其的view是backgroundview
    override func loadView() {
        
        view = backgroundView
        
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        setUpUI()
        
    }
    
    //viewdidappear加载动画
    
    override func viewDidAppear(animated: Bool) {
        //super
        super.viewDidAppear(true)
        startAnimation()
        
    }
    
    //添加约束
    func setUpUI() {
        //添加子控件
        view.addSubview(headView)
        view.addSubview(welcomeLabel)
        headView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSizeMake(85, 85))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(250)
            
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headView.snp_bottom).offset(20)
            make.centerX.equalTo(headView)
            
            
        }
        
        
        }
    
    func startAnimation() {
        
        self.headView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
            
        }
        
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
              UIView.animateWithDuration(1.0, animations: { () -> Void in
                
                self.welcomeLabel.alpha = 1
                
                }, completion: { (_) -> Void in
                    
                    
                    
              })
                
        }
        
    }
    
    
    
        
    }
    
    
    


